home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / RLaB / rlib / intersection.r < prev    next >
Encoding:
Text File  |  1994-10-08  |  781 b   |  45 lines  |  [TEXT/RLAB]

  1. //-------------------------------------------------------------------//
  2. //  intersection
  3.  
  4. //  Syntax:    intersection ( A , B )
  5.  
  6. //  Description:
  7.  
  8. //  Intersection finds the intersection-set of the two vector sets A,
  9. //  and B.
  10.  
  11. //  See Also: complement, set, union
  12.  
  13. //-------------------------------------------------------------------//
  14.  
  15. intersection = function ( A , B )
  16. {
  17.   if(A.nr == 0 || B.nr == 0) 
  18.   {
  19.     return [];
  20.   }
  21.  
  22.   if (min (size (A)) != 1) 
  23.   {
  24.     error ("intersection: 1st arg must be a vector");
  25.   }
  26.  
  27.   if (min (size (B)) != 1) 
  28.   {
  29.     error ("intersection: 2st arg must be a vector");
  30.   }
  31.  
  32.   a = set (A); b = set (B);
  33.   j = 1;
  34.   for (i in 1:b.n)
  35.   {
  36.     tmp = find (b[i] == a);
  37.     if (tmp.n > 0) 
  38.     {
  39.       Int[j] = b[i];
  40.       j++;
  41.     }
  42.   }
  43.   return Int
  44. };
  45.